
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Administrator
 */
public class Screen extends GameCanvas implements Runnable {
    private final MailClientMobi midlet;
    private boolean isPlay;
    private Thread thread;
    private long delay=70;
    private LayerManager layerManager;
    private ImageBuffer[] imageBuffers;
    private Image bgImg;
    private final int MENU_INDEX = 0;
    private final int ALL_INDEX = 1;
    private final int INBOX_INDEX = 2;
    private final int COMPOSE_INDEX = 3;
    private final int SENT_INDEX = 4;
    private final int TRASH_INDEX = 5;
    private final int SETTING_INDEX = 6;

    private final int SUB_LOGIN_INDEX = -1;
    private final int SUB_LIST_INDEX = -3;
    private final int SUB_MENU_INDEX = -2;
    private final int SUB_READ_INDEX = -4;


    private int nextIndex = 0;
    private int indexMain = 0;
    private int indexSub = 0;

    private int menuIndex;
    private int listIndex;
    private int pageIndex;
    private int iconListIndex;
    private int subIndex;
    private int mangaIndex;
    private int choiceIndex = 1;
    private int viewPortX;
    private int viewPortY;
    private int index;
     private boolean requesting = false;
        DataHandler dataHandler;


    public Screen (MailClientMobi midlet,DataHandler dataHandler) throws IOException{
        super(false);

        setFullScreenMode(true);

        this.midlet = midlet;
            this.dataHandler = dataHandler;
        initMenu();
       

//        indexMain = READ_INDEX;
//        nextIndex = READ_INDEX;
        indexMain = MENU_INDEX;
        display();
        viewPortX=0;
        viewPortY=0;
    }

  private void initMenu() throws IOException{
        //viewPortX = preViewMenuX;
        //viewPortY = preViewMenuY;
        layerManager = new LayerManager();
        imageBuffers = new ImageBuffer[6];
        String[] str = new String[imageBuffers.length*2];
        str[0]="ALL MAIL";
        str[1] = "INBOX";
        str[2] = "COMPOSE";
        str[3] = "SENT";
        str[4] = "TRASH";
        str[5]="SETTING";

        str[6] = "/Image/all_mail.png";
        str[7] = "/Image/inbox.png";
        str[8] = "/Image/compose.png";
        str[9] = "/Image/sent_mail.png";
        str[10] = "/Image/trash.png";
        str[11]="/Image/setting.png";

        for(int i=0;i<imageBuffers.length;i++){

            int dx = 80*(i%3)+20;
            int dy = 30+ 90*(i/3);
            imageBuffers[i] = new ImageBuffer(str[i+6], 1, 1, dx, dy,str[i]);
            imageBuffers[i].addLayer(layerManager, -1);
        }


       // bgImg = Image.createImage("/Image/00.png");
      //  regBgWidth = Math.min(getWidth(), bgImg.getWidth());
      //  regBgHeight = Math.min(getHeight(), bgImg.getHeight());

    }
  protected void keyPressed(int keyCode) {
        super.keyPressed(keyCode);
        /*
        if(thread==null)
            return;
        if(indexSub==SUB_READ_INDEX){
            subReadPress(keyCode);
            return;
        }
        if(indexSub==SUB_LIST_INDEX){
            subListPress(keyCode);
            return;
        }
        if(indexSub==SUB_MENU_INDEX){
            subMenuPress(keyCode);
            return;
        }
        if(indexSub==SUB_LOGIN_INDEX){
            subLoginPress(keyCode);
            return;
        }
        if(indexMain==READ_INDEX){
            try {
                readKeyPress(keyCode);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return;
        }
        if(indexMain==LOGIN_INDEX){
            try {
                loginKeyPress(keyCode);
                return;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

         */

        if(indexMain==MENU_INDEX){
            try {
                menuKeyPress(keyCode);
                return;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
         /*
        if(indexMain==LIST_INDEX){
            try {
                listKeyPress(keyCode);
                return;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        if(indexMain==STORE_INDEX){
            storeKeyPress(keyCode);
        }
         *
         */
    }

   private void menuKeyPress(int keyCode) throws IOException{
        int getGameAction = KeyCodeAdapter.getInstance().adoptKeyCode(keyCode);
        if(!requesting){
            switch(getGameAction){
                case KeyCodeAdapter.UP_KEY:
                    menuIndex-=3;
                    if(menuIndex<0){
                        menuIndex = (imageBuffers.length-1)/3*3+2+(menuIndex+3)%3-2;
                        if(menuIndex>imageBuffers.length-1)
                            menuIndex-=3;
                        System.out.println("menuIndex:"+menuIndex);
                    }
                    return;
                case KeyCodeAdapter.DOWN_KEY:
                    menuIndex+=3;
                    if(menuIndex>imageBuffers.length-1){
                        menuIndex = menuIndex%3;

                    }
                      System.out.println("menuIndex:"+menuIndex);
                    return;
                case KeyCodeAdapter.LEFT_KEY:
                    menuIndex--;
                    if(menuIndex<0){
                        menuIndex = imageBuffers.length-1;
                    }
                      System.out.println("menuIndex:"+menuIndex);
                    return;
                case KeyCodeAdapter.RIGHT_KEY:
                    menuIndex++;
                    if(menuIndex>imageBuffers.length-1){
                        menuIndex = 0;
                    }
                      System.out.println("menuIndex:"+menuIndex);
                    return;
                case KeyCodeAdapter.CENTER_KEY:
                    if(menuIndex==0){
                        requesting = true;
                     //   timeSchedule = System.currentTimeMillis();
                     //   DataHandler.requestData(DataHandler.REQUEST_LIST,pageIndex);
                    }
                    else if(menuIndex==2){
                        nextIndex = COMPOSE_INDEX;
                        indexMain = COMPOSE_INDEX;
                     //   clean = CLEAN_MENU;
                        thread = null;
                    }
                      System.out.println("menuIndex:"+menuIndex);
                    return;
                case KeyCodeAdapter.SOFT_KEY_RIGHT:
                    return;
                case KeyCodeAdapter.SOFT_KEY_LEFT:
                    indexSub = SUB_MENU_INDEX;
//                    indexMain =LOGIN_INDEX;
//                    clean = CLEAN_MENU;
//                    menuIndex = 0;
//                    gameThread = null;
                    return;
            }
        }

    }

  private void drawMenu(Graphics g){
        //viewX(menuIndex);
       // viewY(menuIndex);
        layerManager.setViewWindow(viewPortX, viewPortY, getWidth(), getHeight());
      //  g.setColor(0xff075982);
    // g.setColor(0x150517);

       g.setColor(0xff262600);
      
        g.fillRect(0, 0, getWidth(), getHeight());

//        g.drawRegion(bgImg, 0, 0, regBgWidth, regBgHeight, 0, 0, 0, 0);

//        Designer.gradientBox(g, color_gradien[0], color_gradien[1], imageBuffers[menuIndex].getX()-viewPortX, imageBuffers[menuIndex].getY()-viewPortY,
//                imageBuffers[menuIndex].getWidth(),imageBuffers[menuIndex].getHeight(), 0, 4);
      
  
       
     // g.setColor(0x1589FF);
      //   g.fillRect(0,index , getWidth(), 30);
        for(int i=0;i<imageBuffers.length;i++){
           
                int height,color;
                height = 20;
               color = 4;
                imageBuffers[i].setFrame(0);
                imageBuffers[i].drawText(g,color,height,viewPortX,viewPortY);
                if(menuIndex==i)
                {
                    g.setColor(0x1589FF);
                   // imageBuffers[i].drawRec(g,0x1589FF , viewPortX, viewPortY);
                    g.drawRect(imageBuffers[i].getX()-viewPortX, imageBuffers[i].getY()-viewPortY, imageBuffers[i].getWidth(), imageBuffers[i].getHeight());
               }
                

            
           }
         layerManager.paint(g, 0, 0);

        
         
         
         
           // Designer.drawString(g, String.valueOf(getHeight()), 0, 100, 4, 200, 200);
        


     //   gradientBox(g, color_gradien[0],color_gradien[1], 0, 0, getWidth(),20, 0, 0);
      //  gradientBox(g, color_gradien[0],color_gradien[1], 0, getHeight()-20,getWidth(),20, 0, 0);
        Designer.drawString(g, "MENU", 0, 100, 4, 10, getHeight()-20);

      //  Designer.drawString(g, "INBOX", 0,100,4,50,20 );
      

    }
     private void display(){
        start();
        midlet.display.setCurrent(this);
    }

      private void start(){
        isPlay = true;
        thread = new Thread(this);
        thread.start();
    }
      public void run() {
        Graphics g = this.getGraphics();
        Thread currentThread = Thread.currentThread();
        while(currentThread == thread){
            long startTime = System.currentTimeMillis();
            if(isShown()){
                if(isPlay){
                    /*

                    if(indexMain==LOGIN_INDEX){
                        drawLogin(g);
                    }
                    else*/

                    if(indexMain==MENU_INDEX){
                        drawMenu(g);
                    }
                    
                    else if(indexMain==COMPOSE_INDEX){
                        midlet.showForm();
                    }
                    /*
                    else if(indexMain==READ_INDEX){
                        drawRead(g);
                    }
                    else if(indexMain==STORE_INDEX){
                        drawStore(g);
                    }
                    if(requesting){
                        drawRequesting(g);
                    }
                    if(indexSub!=0){
                        drawSubMenu(g);
                    }
                    */
                    flushGraphics();
                    Runtime.getRuntime().gc();
                }
            }
            long timeTake = System.currentTimeMillis() - startTime;
            if(timeTake < delay){
                synchronized(this){
                    try {
                        wait(delay - timeTake);
                    } catch (InterruptedException ex) {
                    }
                }
            }
            else {
                    Thread.yield();
            }
        }
        cleanScreen();
        try {
            initScreen();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        this.start();
    }

       private void initScreen() throws IOException{
           /*
        if(indexMain==LOGIN_INDEX){
            initLogin();
        }
        else if(indexMain==MENU_INDEX){
            initMenu();
        }
        else if(indexMain==LIST_INDEX){
            initList();
        }
        else if(indexMain== STORE_INDEX){
            initStore();
        }*/
    }
       private void cleanScreen(){
           /*
        if(clean==0){
            return;
        }
        if(clean==CLEAN_LOGIN){
            cleanLogin();
        }
        else if(clean==CLEAN_MENU){
            cleanMenu();
        }
        else if(clean==CLEAN_LIST){
            cleanList();
        }
        else if(clean==CLEAN_STORE){
            cleanStore();
        }
        else if(clean==CLEAN_READER){
            cleanRead();
        }
        clean = 0;*/
    }

        private void cleanMenu(){
            /*
        layerManager = null;
        imageBuffers = null;
        preViewMenuX = viewPortX;
        preViewMenuY = viewPortY;
             *
             */

    }


    }



